package lsf.itcast.controller;

import com.github.pagehelper.PageInfo;
import lsf.itcast.domain.Product;
import lsf.itcast.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProdunctController {
    @Autowired
    private ProductService productService;



    //保存新增产品信息
    @RequestMapping("/save.do")
    public String saveProduct(Product product){
        productService.saveProduct(product);
        return "redirect:findAll.do";
    }


    //查询所有产品信息
    @RequestMapping("/findAll.do")
    //方法的权限控制（第三步）
    @RolesAllowed("ADMIN")
    public ModelAndView findAllProduct(/*分页后*/@RequestParam(name ="page",required =true,defaultValue ="1")Integer page,
                                              @RequestParam(name ="size",defaultValue ="4")Integer pageSize){
        ModelAndView mv = new ModelAndView();
        List<Product> products = productService.findAllProdunct(page,pageSize);
      // mv.addObject("productList",products);
       //mv.setViewName("product-list");
        PageInfo pageInfo = new PageInfo(products);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("product-list");
        return mv;
    }
}
